import datetime
from django.http import JsonResponse
from core.tools.models import Province
from apps.product.models import Product, ProductCityRelation
from core.tools.wx_login_verify import wx_login
from finance_c2c.local_settings import Config
from core.tools.wx_session import get_wx_session

@wx_login()
def products(request):
    wx_obj = get_wx_session(request)
    channel = wx_obj.salesman.channel
    is_show_cash = channel.is_show_cash  # 是否展示佣金
    info = Product.objects.filter(status=True, channelproduct__channel=channel, channelproduct__status=True)
    p_type = request.GET.get("type")
    limit = request.GET.get("limit")
    parent_product = request.GET.get("parent_product")
    if p_type:
        info = info.filter(type=p_type)
    if parent_product:
        info = info.filter(parent_product_id=parent_product)
    else:
        info = info.filter(list_show=True)
        # info = info.filter(parent_product__isnull=True)
    if limit:
        info = info[0:int(limit)]
    product_list = []
    for i in info:
        d = dict()
        d["id"] = i.id
        d["pk"] = i.id
        d["name"] = i.name
        d["special"] = i.special  # 副标题
        d["adv"] = i.adv.split(";")  # 广告语
        if i.list_logo:
            d["list_logo"] = Config.host + i.list_logo.url
            d["list_logo"] = "//c2c.kalaibao.com" + i.list_logo.url
        else:
            d["list_logo"] = ""
        if i.index_logo:
            d["index_logo"] = Config.host + i.index_logo.url
            d["index_logo"] = "//c2c.kalaibao.com" + i.index_logo.url
        else:
            d["index_logo"] = ""
        if i.img:
            d["img"] = "//c2c.kalaibao.com" + i.img.url
        else:
            d["img"] = ""
        try:
            now_time = datetime.datetime.now()
            now_time = now_time.strftime("%Y-%m-%d 23:59:59")
            price_info = i.channelproduct_set.filter(channel=channel).first().priceupdate_set.filter(
                update_time__lte=now_time).first()
            price_type = price_info.price_type
            price1 = price_info.price
            price2 = price_info.leader_price
            price3 = price_info.leader_leader_price
            price = price1 + price2 + price3
            if price_type == 1:
                d["price_type"] = "共奖励（元）"
                d["price_sum"] = '%.0f' % price + "元"
                d["price1"] = '客户补贴：%.2f' % price1 + "元"
                d["price2"] = '推荐奖励：%.2f' % price2 + "元"
                d["price3"] = '导师奖励：%.2f' % price3 + "元"
            else:
                d["price_type"] = "共奖励（%）"
                d["price_sum"] = '%.2f' % (price * 100)
                d["price1"] = '客户补贴：%.2f' % (price1 * 100)
                d["price2"] = '推荐奖励：%.2f' % (price2 * 100)
                d["price3"] = '导师奖励：%.2f' % (price3 * 100)
        except Exception as e:
            print(e)
            price = 0
        if is_show_cash:
            d["price_is_0"] = price
        else:
            d["price_is_0"] = 0
        d["jump_status"] = i.jump_status
        d["is_firm"] = i.is_firm
        d["sign"] = i.sign
        d["url"] = i.url
        if i.product_set.exists() and i.sign_type == 4:
            parent_product = "true"
        else:
            parent_product = "false"
        d["parent_product"] = parent_product
        d["api_type"] = i.api_type
        d["sign_type"] = i.sign_type
        d["jump_status"] = i.jump_status
        product_list.append(d)
    context = {"product_list": product_list}
    return JsonResponse(context)


def product_province(request):
    """
    获取省份
    :param request:
    :return:
    """
    product_id = request.GET.get("p_pk")
    province_info = Province.objects.filter(productcityrelation__product_id=product_id)
    province_list = []
    for i in province_info:
        d = dict()
        d["name"] = i.name
        d["pk"] = i.pk
        if d not in province_list:
            province_list.append(d)
    content = {"province_list": province_list}
    return JsonResponse(content)


def product_city(request):
    """
    根据产品和省份获取城市
    :param request:
    :return:
    """
    product_id = request.GET.get("p_pk")
    province_id = request.GET.get("pro_pk")
    info = ProductCityRelation.objects.filter(product_id=product_id, province_id=province_id)
    city_list = []
    for i in info:
        d = dict()
        d["name"] = i.city.name
        d["pk"] = i.city.pk
        city_list.append(d)
    content = {"city_list": city_list}
    return JsonResponse(content)

